{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-05T01:00:04.268888Z",
     "start_time": "2025-03-05T01:00:04.265938Z"
    }
   },
   "source": [
    "class Adder:\n",
    "    def __init__(self, initial_value=0):\n",
    "        # 初始化一个初始值\n",
    "        self.value = initial_value\n",
    "\n",
    "    def __call__(self, x):\n",
    "        # 当实例被调用时，将传入的值加到初始值上\n",
    "        # self.value += x\n",
    "        return self.value + x\n",
    "\n",
    "\n",
    "# 创建一个 Adder 的实例\n",
    "adder = Adder(10)  # 类里面重写了__call__方法，所以可以像函数一样调用实例，执行call方法\n",
    "\n",
    "# 调用实例，就像调用函数一样\n",
    "print(adder(5))  # 输出: 15\n",
    "print(adder(3))  # 输出: 18\n",
    "print(adder(2))  # 输出: 20"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "13\n",
      "12\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
